package com.dhcc.bpm.alarm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.alarm.entity.AlarmNotice;
import com.dhcc.bpm.alarm.entity.AlarmNoticeTarget;
import com.dhcc.bpm.alarm.mapper.AlarmNoticeInfoMapper;
import com.dhcc.bpm.alarm.service.IAlarmNoticeInfoService;
import com.dhcc.bpm.alarm.service.IAlarmNoticeTargetService;
import com.dhcc.bpm.alarm.vo.AlarmNoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiafayu
 * @since 2022-02-16
 */
@Service("noticeServiceImpl1")
public class AlarmNoticeInfoServiceImpl extends ServiceImpl<AlarmNoticeInfoMapper, AlarmNotice> implements IAlarmNoticeInfoService {
    
	@Autowired
    AlarmNoticeInfoMapper alarmNoticeInfoMapper;
    
    @Autowired
    IAlarmNoticeTargetService alarmNoticeTargetService;

    @Override
    public IPage<AlarmNoticeVO> selectAlarmNoticePage(IPage<AlarmNoticeVO> page, AlarmNoticeVO AlarmNotice) {
        return page.setRecords(baseMapper.selectAlarmNoticePage(page, AlarmNotice));
    }

    @Transactional(rollbackFor = Exception.class)
	@Override
	public boolean saveOrUpdate(AlarmNotice alarmNotice, List<AlarmNoticeTarget> alarmNoticeTargetList) {
    	
    	if(null != alarmNotice.getId()) {
    		alarmNotice.setNoticeCode(null);// 修改时编码不做修改
    	}
    	
    	super.saveOrUpdate(alarmNotice);
    	
    	// 清除关联关系表数据
    	alarmNoticeTargetService.removeByNoticeIds(Arrays.asList(alarmNotice.getId()));
	
    	// 插入关联关系
    	if(null != alarmNoticeTargetList &&  alarmNoticeTargetList.size()>0) {
    		for(AlarmNoticeTarget alarmNoticeTarget : alarmNoticeTargetList) {
    			alarmNoticeTarget.setNoticeId(alarmNotice.getId());
    		}
    		alarmNoticeTargetService.saveBatch(alarmNoticeTargetList);
    	}
    	
		return true;
	}
    
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(List<Long> idList) {
    	alarmNoticeTargetService.removeByNoticeIds(idList);
    	return super.removeByIds(idList);
    }
    
    
}
